package com.yy.micro.magic.data.model.converter;

import com.yy.micro.magic.data.model.utils.JsonUtils;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.AttributeConverter;

public class TypedListConverter implements AttributeConverter<List<String>,String> {
    @Override
    public String convertToDatabaseColumn(List<String> attribute) {
        if (null == attribute || attribute.isEmpty()){
            return null;
        }
        return JsonUtils.toJson(attribute);
    }

    @Override
    public List<String> convertToEntityAttribute(String dbData) {
        if (dbData == null || dbData.trim().length() == 0) {
            return new ArrayList();
        }
        return JsonUtils.fromJson(dbData,List.class);
    }
}
